package com.yuejian_06.yuejian_06.model;

import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.bean.CourseBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.fragment.Constants_l;
import com.yuejian_06.yuejian_06.service.CourseService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by songedwin on 2017/10/18.
 */

public class CourseModel {

    private Retrofit retrofit;
    public CourseModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.SERVERADDRESS)//增加返回值为Gson的支持（一实体类返回）
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //课表列表
    public void courseList( final LoginListener loginListener){
        CourseService courseService = retrofit.create(CourseService.class);
        Call<List<CourseBean>> call=courseService.courseList();
        call.enqueue(new Callback<List<CourseBean>>() {
            @Override
            public void onResponse(Call<List<CourseBean>> call, Response<List<CourseBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<CourseBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }
    //课表列表---根据等级查询
    public void getcourseStar( String star,final LoginListener loginListener){
        CourseService courseService = retrofit.create(CourseService.class);
        Call<List<CourseBean>> call=courseService.getcourseStar(star);
        call.enqueue(new Callback<List<CourseBean>>() {
            @Override
            public void onResponse(Call<List<CourseBean>> call, Response<List<CourseBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<CourseBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }
    //课表列表---课程搜索
    public void getcourseSearch( String name,final LoginListener loginListener){
        CourseService courseService = retrofit.create(CourseService.class);
        Call<List<CourseBean>> call=courseService.getcourseSearch(name);
        call.enqueue(new Callback<List<CourseBean>>() {
            @Override
            public void onResponse(Call<List<CourseBean>> call, Response<List<CourseBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<CourseBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //课程详情
    public void courseInfo( String course_id,final LoginListener loginListener){
        CourseService courseService = retrofit.create(CourseService.class);
        Call<CourseBean> call=courseService.getcourse(course_id);
        call.enqueue(new Callback<CourseBean>() {
            @Override
            public void onResponse(Call<CourseBean> call, Response<CourseBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<CourseBean> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //课程下订单
    public void courseOrder(String user_id, String course_id,
                    String sign_name, String IDcard,String sum_price,String sign_tele,
                    final LoginListener loginListener){
        CourseService courseService = retrofit.create(CourseService.class);
        Call<SuccessBean> call=courseService.courseOrder(user_id,course_id,sign_name,IDcard,sum_price,sign_tele);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }




}
